package com.th.cash.th2.splash

import androidx.activity.viewModels
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.lxj.xpopup.XPopup
import com.th.basemodel.base.BaseVB2Activity2
import com.th.basemodel.base.BaseViewModel
import com.th.basemodel.ext.gotoActivity
import com.th.resmodel.databinding.ActivitySplashBinding
import com.th.cash.storage.LocalSetting
import com.th.cash.th2.main.MainActivity

//启动页
class SplashActivity : BaseVB2Activity2<ActivitySplashBinding>() {

    private val mViewModel by viewModels<SplashViewModel>()

    override fun getVm(): BaseViewModel {
        return mViewModel
    }

    override fun setWindowPadding() {
        ViewCompat.setOnApplyWindowInsetsListener(mBinding.main) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, 0, systemBars.right, systemBars.bottom)
            insets
        }
    }

    override fun init() {
        initTitle()
        if (LocalSetting.firstUsed) {
            showDisclosurePopup()
        } else {
            mBinding.root.postDelayed({
                gotoActivity<MainActivity>()
                finish()
            }, 2000)
        }
        mViewModel.intentChannel.trySend(SplashIntent.Idle)
    }

    private fun initTitle() {
//        mBinding.header.headTitle.text = ""
    }

    private fun showDisclosurePopup() {
        XPopup.Builder(this)
            .dismissOnBackPressed(false)
            .dismissOnTouchOutside(false)
            .asCustom(DisclosurePopup(this).run {

                clickListener = { agree ->
                    LocalSetting.firstUsed = !agree

                    gotoActivity<MainActivity>()
                    finish()
                }

                this
            })
            .show()
    }
}